#include "widget.h"
#include "ui_widget.h"
#include"QDebug"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    a.clear();
    b.clear();
    //绑定运算符函数
    connect(ui->pushButton0,&QPushButton::clicked,[=](){btn_logic(0);});
    connect(ui->pushButton1,&QPushButton::clicked,[=](){btn_logic(1);});
    connect(ui->pushButton_2,&QPushButton::clicked,[=](){btn_logic(2);});
    connect(ui->pushButton_3,&QPushButton::clicked,[=](){btn_logic(3);});
    connect(ui->pushButton_4,&QPushButton::clicked,[=](){btn_logic(4);});
    connect(ui->pushButton_5,&QPushButton::clicked,[=](){btn_logic(5);});
    connect(ui->pushButton_6,&QPushButton::clicked,[=](){btn_logic(6);});
    connect(ui->pushButton_7,&QPushButton::clicked,[=](){btn_logic(7);});
    connect(ui->pushButton_8,&QPushButton::clicked,[=](){btn_logic(8);});
    connect(ui->pushButton_9,&QPushButton::clicked,[=](){btn_logic(9);});
    connect(ui->jia,&QPushButton::clicked,[=](){btn_logic(0,"+");});
        connect(ui->jian,&QPushButton::clicked,[=](){btn_logic(0,"-");});
        connect(ui->cheng,&QPushButton::clicked,[=](){btn_logic(0,"*");});
        connect(ui->chu,&QPushButton::clicked,[=](){btn_logic(0,"/");});
        connect(ui->dengyu,&QPushButton::clicked,[=](){btn_logic(0,"=");});
            //对lineedit进行更新
            connect(ui->guiling,&QPushButton::clicked,[=](){a.clear();b.clear();});
            //tuige
              connect(ui->tuige,&QPushButton::clicked,[=](){
                a.chop(1);
                ui->lineEdit->setText(a);
              });
          }
void Widget:: btn_logic(int x, QString i)
{
    if(i==" ")
    {
        a+=QString::number(x);
    }
    else if(x==0) {
        if(i!="="){
            b.insert(0,a);
            a.clear();
            b.insert(1,i);

        }
        else
        {
            b.insert(2,a);
            deng_logic();

        }
        //如果输入的为运算符，将lineedit内容存储到b【0】运算符存储到b【1】
        //判断运算符

        }

    //显示lineedit
    ui->lineEdit->setText(a);
    }
void Widget::deng_logic()
{
    a.clear();
    if(b.at(1)=="+"){
        a+=QString::number(QString(b.at(0)).toInt()+QString(b.at(2)).toInt());
    }else if(b.at(1)=="-"){
        a+=QString::number(QString(b.at(0)).toInt()-QString(b.at(2)).toInt());

    }else if(b.at(1)=="*"){
        a+=QString::number(QString(b.at(0)).toInt()*QString(b.at(2)).toInt());

    }else if(b.at(1)=="/"){
        a+=QString::number(QString(b.at(0)).toInt()/QString(b.at(2)).toInt());

    }
    ui->lineEdit->setText(a);
}
Widget::~Widget()
{
    delete ui;
}




